<template>
 <div class='container'>
   <el-card class="box-card">
<!-- 新增按钮与删除按钮 -->
 <el-row>
    <el-col>
    <!-- <el-col :span="1" :offset="21">  -->
    <el-button class="buts" type="success" size="small" @click="add()">添加试题</el-button>
    </el-col>
 </el-row>
<!-- 表格区域——————————————————————————————————————————————————————————————————start -->
 <el-form ref="form" :model="form" label-width="80px" :inline="true" class="demo-form-inline">
   <!-- 第一排开始 ----------------------------------------------start-->
  <el-row>
     <el-col :span="6">
       <el-form-item label="学科" size='mini'>
         <el-select v-model="form.subjectID" placeholder="请选择" @change="variation">
      <el-option
      v-for="items in form.serchList.data"
      :label="items.label"
      :value="items.value"
      :key="items.value"
      >
      </el-option>
         </el-select>
       </el-form-item>
      </el-col>
    <el-col :span="6">
    <el-form-item label="二级目录" size='mini'>
    <el-select v-model="form.catalogID" placeholder="请选择">
      <el-option
      v-for='items in form.catalogList'
      :value="items.label"
      :key="items.value"
      >
      </el-option>
    </el-select>
  </el-form-item>
  </el-col>
  <el-col :span="6">
    <el-form-item label="标签" size='mini'>
      <el-select v-model="form.tags" placeholder="请选择" size='mini'>
        <el-option
        v-for="(items,index) in form.dataList"
        :label="items.tags"
        :value="items.tags"
        :key="index"
        >
        </el-option>
      </el-select>
     </el-form-item>
  </el-col>
  <el-col :span="6">
     <el-form-item label="关键字" size='mini'>
        <el-input v-model="form.keyword" class="cpx"></el-input>
     </el-form-item>
  </el-col>
  <el-col :span="6">
      <el-form-item label="试题类型" size='mini'>
        <el-select v-model="form.questionType" placeholder="请选择" size='mini'>
          <el-option
          v-for='(items,idx) in form.dataList'
          :label="items.question"
          :value="items.questionType"
          :key="idx"
          >
          </el-option>
        </el-select>
      </el-form-item>
  </el-col>
  <el-col :span="6">
    <el-form-item label="难度" size='mini'>
    <el-select v-model="form.difficulty" placeholder="请选择" size='mini'>
          <el-option
          v-for='items in difficultys'
          :key="items.value"
          :value="items.value"
          :label="items.label"
          >
          </el-option>
       </el-select>
  </el-form-item>
  </el-col>
  <el-col :span="6">
      <el-form-item label="方向" size='mini'>
    <el-select v-model="form.direction" placeholder="请选择">
      <el-option
        v-for='items in  directions'
        :key="items"
        :label="items"
        :value="items"
      >
      </el-option>
    </el-select>
  </el-form-item>
  </el-col>
  <el-col :span="6">
      <el-form-item label="录入人" size='mini'>
    <el-select v-model="form.creator" placeholder="请选择" >
      <el-option
      v-for='items in form.dataList'
      :label="items.creator"
      :value="items.creatorID"
      :key="items.creatorID"
      >
      </el-option>
    </el-select>
  </el-form-item>
  </el-col>
  <el-col :span="6">
    <el-form-item label="题目备注" size='mini'>
    <el-input v-model="form.remarks" class="cpx"></el-input>
  </el-form-item>
  </el-col>
  <el-col :span="6">
    <el-form-item label="企业简称" size='mini'>
    <el-select v-model="form.enterpriseID" placeholder="请选择" >
      <el-option
      v-for='items in form.dataList'
      :label="items.enterprise"
      :value="items.enterpriseID"
      :key="items.enterpriseID"
      >
      </el-option>
    </el-select>
  </el-form-item>
  </el-col>
    <el-col :span="5">
      <el-form-item label="城市" size='mini'>
        <el-select v-model="form.city" placeholder="活动区域"  style="width:80px" @change="tabCity">
          <el-option
          v-for="items in cityList"
          :label="items"
          :value="items"
          :key="items"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" label-width='1px' size='mini'>
         <el-select v-model="form.resource" placeholder="活动区域"  style="width:80px">
           <el-option v-for="item in resourceList"
            :label="item"
            :key="item"
            :value="item">
            </el-option>
         </el-select>
      </el-form-item>
  </el-col>
  <el-col :span="3" :offset="4">
     <el-form-item>
      <el-button @click="resetForm('form')" size='small'>重置</el-button>
      <el-button type="primary" @click="submitForm" size='small' v-loading="loading">搜索</el-button>
     </el-form-item>
  </el-col>
</el-row>
<!-- 第一排结束 ----------------------------------------------end-->
</el-form>
<!-- 表格区域——————————————————————————————————————————————————————————————————end -->
<!-- 选项卡区域——————————————————————————————————————————————————————————————————start -->
 <div>
    <el-radio-group v-model="State" @change="changes">
      <el-radio-button :label="null">全部</el-radio-button>
      <el-radio-button label="0">待审核</el-radio-button>
      <el-radio-button label="1">通过</el-radio-button>
      <el-radio-button label="2">拒绝</el-radio-button>
    </el-radio-group>
  </div>
<!-- 选项卡区域——————————————————————————————————————————————————————————————————end -->
<el-alert
    :title="number"
    type="info"
    show-icon>
  </el-alert>
<!-- 列表区域——————————————————————————————————————————————————————————————————start -->
 <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      prop="number"
      label="试题编号"
      width="150">
    </el-table-column>
    <el-table-column
      prop="subject"
      label="学科"
      width="120">
    </el-table-column>
    <el-table-column
      prop="catalog"
      label="目录"
      width="120">
    </el-table-column>
    <el-table-column
      label="题型"
      width="120">
      <template slot-scope="scope">
         <span v-if="Number(scope.row.questionType) === 1">{{ '单选' }}</span>
         <span v-else-if="Number(scope.row.questionType) === 2">{{ '多选' }}</span>
         <span v-else-if="Number(scope.row.questionType) === 3">{{ '简答' }}</span>
      </template>
    </el-table-column>
    <el-table-column
      label="题干"
      width="200">
      <template slot-scope="scope">
        <span v-html=scope.row.question></span>
      </template>
    </el-table-column>
    <el-table-column
      prop="addDate"
      label="录入时间"
      width="150">
       <template slot-scope="scope">
         <span>{{scope.row.addDate | parseTimeByString("{y}-{m}-{d} {h}:{m}:{s}")}}</span>
      </template>
    </el-table-column>
   <el-table-column
      label="难度"
      width="100">
       <template slot-scope="scope">
         <span v-if="Number(scope.row.difficulty) === 1">{{ '简单' }}</span>
         <span v-else-if="Number(scope.row.difficulty) === 2">{{ '普通' }}</span>
         <span v-else-if="Number(scope.row.difficulty) === 3">{{ '困难' }}</span>
      </template>
    </el-table-column>
    <el-table-column
      prop="creator"
      label="录入人"
      width="200">
    </el-table-column>
    <el-table-column
      label="发布状态"
      width="120">
      <template slot-scope="scope">
         <span v-if="Number(scope.row.publishState) === 1">{{ '下架' }}</span>
         <span v-else-if="Number(scope.row.publishState) === 0">{{ '上架' }}</span>
      </template>
    </el-table-column>
    <el-table-column
      fixed="right"
      label="操作"
      width="200">
      <template
       slot-scope="scope"
      >
    <el-row style="margin-top: 20px"
    >
         <el-button type="text" size='mini' @click="preview(scope.row.id)">预览</el-button>
         <el-button type="text" size='mini' @click="openAduit(scope.row.id)">审核</el-button>
         <el-button type="text" size='mini' @click="rootClick(scope.row.id)">修改</el-button>
         <el-button type="text" size='mini' @click="upAandD(scope.row.id,scope.row)">{{scope.row.publishState===0?"下架":"上架"}}</el-button>
         <el-button type="text" size='mini' @click="delects(scope.row.id)">删除</el-button>
     </el-row>
    </template>
    </el-table-column>
</el-table>
<!-- 列表区域——————————————————————————————————————————————————————————————————end -->
<!-- 分页区域——————————————————————————————————————————————————————————————————start -->
 <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="callpage"
      :page-sizes="[10,20,30,50]"
      :page-size="pagess"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tol">
    </el-pagination>
  </div>
<!-- 分页区域——————————————————————————————————————————————————————————————————end -->
<!-- 审核区域---------------------------------------------------------------------- -->
<el-dialog title="题目审核" :visible.sync="dialogFormVisible">
  <el-form :model="form" :rules="rules">
  <el-radio v-model="form.chkState" label="1">通过</el-radio>
  <el-radio v-model="form.chkState" label="2">不通过</el-radio>
  <!-- 文本域 -->
   <el-form-item label=""  prop="name">
     <el-input
     style="margin-top:10px"
     type="textarea"
     :rows="2"
     placeholder="请输入审核意见"
     v-model="form.chkRemarks">
  </el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="postAd(adId)">确 定</el-button>
  </div>
</el-dialog>
<!-- 审核区域---------------------------------------------------------------------- -->
<!-- 预览区域 -->
<el-dialog title="预览页面" :visible.sync="log">
  <Prev :datas="fomsId"/>
  <div slot="footer" class="dialog-footer">
    <el-button @click="log = false">取 消</el-button>
    <el-button type="primary" @click="log = false">确定</el-button>
  </div>
</el-dialog>
<!-- 预览区域 -->
   </el-card>
 </div>
</template>

<script>
import Prev from '@/module-hmmm/components/prev.vue'
import { choice, remove, choiceCheck as pAd, choicePublish } from '@/api/hmmm/questions.js'
import { provinces, citys as getCity } from '@/api/hmmm/citys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { simple as sim } from '@/api/hmmm/directorys.js'
export default {
  name: 'finePaper',
  created () {
    this.loadBouTique()
    this.getTwoObjct()
  },
  computed: {
    number () {
      return `一共有${this.tol}条数据`
    }
  },
  mounted () {
    // provinces()
    // console.log(provinces())
    this.cityList = provinces()
    console.log(this.cityList)
    this.type()
  },
  data () {
    return {
      name: 'questionChoice',
      log: false,
      rules: {
        name: { required: true, message: '请输入审核意见', trigger: 'blur' }
      },
      publishState: false,
      // 表格数据
      isRight: '',
      loading: false,
      form: {
        // data 其余总数据
        dataList: [],
        // 学科数据
        subjectID: null,
        // 目录数据
        catalogList: [],
        catalogID: null,
        // 标签
        tags: '',
        // 关键字数据
        keyword: null,
        // 难度
        difficulty: '',
        // 录入人
        creator: '',
        // 试题类型
        questionType: '',
        // 方向
        direction: '',
        remarks: '',
        // 企业
        enterpriseID: '',
        city: '',
        province: '',
        delivery: false,
        resource: '',
        // 审核数据
        chkState: '',
        // 审核建议
        chkRemarks: '',
        serchList: []
      },
      State: null,
      callpage: 1,
      pagess: 10,
      // 列表数据
      // 列表数据
      tableData: [],
      tol: null,
      difficultys: [
        {
          value: 1,
          label: '简单'
        },
        {
          value: 2,
          label: '一般'
        },
        {
          value: 3,
          label: '困难'
        }
      ],
      directions: [
        'o2o',
        '外包服务',
        '企业服务',
        '互联网金融',
        '企业咨询',
        '互联网',
        '电子商务',
        '其他'
      ],
      cityList: [],
      resourceList: [],
      dialogFormVisible: false,
      adID: '',
      fomsdata: [],
      fomsId: null
    }
  },
  components: {
    Prev
  },
  methods: {
    handleClick (tab, event) {
      console.log(tab, event)
    },
    // 渲染数据------------------------------------------------------------------start
    async loadBouTique () {
      const query = {
        page: this.callpage,
        pagesize: this.pagess,
        chkState: this.State,
        subjectID: this.form.subjectID,
        // catalogID: this.form.catalogID,
        // enterpriseID: this.form.enterpriseID,
        // province: this.form.province,
        // city: this.form.city,
        // direction: this.form.direction,
        // questionType: this.form.questionType,
        // difficulty: this.form.difficulty,
        keyword: this.form.keyword
      }
      await choice(query).then((res) => {
        console.log('渲染的数据')
        console.log(res)
        this.tableData = res.data.items
        this.tol = res.data.counts
        // this.pagess=res.pagesize
        // 目录
        // 3.标签
        this.form.dataList = res.data.items.map((item) => {
          return {
            // 标签
            tags: item.tags,
            // 录入人
            creator: item.creator,
            // 方向
            // 难度
            //  difficulty:item.difficulty,
            // 试题类型
            questionType: item.questionType,
            question: item.question,
            // 企业
            enterprise: item.enterprise,
            enterpriseID: item.enterpriseID

          }
        })
        console.log(this.form.dataList)
        // if(this.form.id){
        //  this.form.catalogID=res
        // }
      }).catch((err) => {
        console.log(err)
      })
    },
    // 渲染数据-----------------------------------------------------------end
    // 选项卡功能--------------------------------------------------------start
    changes () {
      this.loadBouTique()
    },
    // 选项卡功能--------------------------------------------------------end
    // 分页功能----------------------------------------------------------srart
    handleSizeChange (val) {
      // console.log(`每页: ${val} 条`);
      this.pagess = val
      this.loadBouTique()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.callpage = val
      this.loadBouTique()
    },
    // 分页功能-------------------------------------------------------------end
    // 城市功能
    tabCity (val) {
      // alert(val)
      this.resourceList = getCity(val)
    },
    // 查询模块-------------------------------------------------------------start
    // 1.先给学科赋值
    // 学科功能
    variation () {
      this.form.catalogID = this.form.catalogList[0].label
    },
    async getTwoObjct () {
    const res = await sim()
    this.form.catalogList = res.data
    }
    ,
    submitForm () {
      this.loading = true
      this.loadBouTique()
      this.loading = false
    },
    // 查询模块-------------------------------------------------------------end
    // 列表增删改查-----------------------------------------------------------start>
    delects (data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id: data })
        console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadCpy()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 审查
    openAduit (data) {
      console.log(data)
      this.adID = data
      this.dialogFormVisible = true
    },
    async postAd () {
      this.dialogFormVisible = false
      const id = this.adID
      const data = {
        id: id,
        chkState: Number(this.form.chkState),
        chkRemarks: this.form.chkRemarks

      }
      const res = await pAd(data)
        .then(() => {
          console.log(res)
          this.$message({
            type: 'success',
            message: '提交成功!'
          })
          this.loadBouTique()
        }
        )
    },
    // 上下架
    upAandD (data, datas) {
      console.log(data, datas.publishState)
      this.$confirm(`此操作将${datas.publishState === 0 ? '下架' : '上架'}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const options = {
          id: data,
          publishState: datas.publishState === 0 ? 1 : 0
        }

        const res = await choicePublish(options).then(
          this.$message(`${datas.publishState === 0 ? '下架' : '上架'}成功`)
        ).catch((err) => {
          this.$message.console.error(err)
        })
        console.log(res)
        this.loadBouTique()
      })
    },
    // 预览页
    preview (id) {
      this.log = true
      this.fomsId = Number(id)
      console.log(this.fomsId)
    },
    // 列表增删改查-----------------------------------------------------------end>
    // 点击修改按钮
    rootClick (id) {
      // console.log('编辑', id)
      this.$router.push({
        path: 'new',
        query: {
          id
        }
      })
    },
    add () {
      this.$router.push('new')
    },

    // 重置按钮
    resetForm (formName) {
      this.$refs[formName].resetFields()
      this.form.subjectID = null
      this.form.keyword = null
      this.loadBouTique()
    },
    // 渲染科目
    async type () {
      const res = await simple()
      console.log(res)
      this.form.serchList = res
    }
  }
}
</script>
<style scope >
.el-radio{
  margin-top:10px ;
  display: block;
}
  .block{
   float: right;
 }
  .cpx{
    width: 200px;
  }
  .headcontes {
    width: 100%;
  }
  .text {
    font-size: 14px;
  }
  .buts {
    margin:0 0 10px 0;
  }
   .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
